package com.mzpai.product.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mzpai.product.model.StoreCategory;
import com.mzpai.product.vo.StoreCategoryMenuVo;

public class StoreCategoryMenuHandler {
	
	public static Map<String, StoreCategoryMenuVo> nodeDataToTree(List<StoreCategory> list) {
        String[] matchs = new String[list.size()];
        String id;
        String parentId;
        int i = 0;
        Map<String, StoreCategoryMenuVo> map = new HashMap<String, StoreCategoryMenuVo>();
        map.put(String.valueOf(0), new StoreCategoryMenuVo());
        map.get(String.valueOf(0)).setChildren(new ArrayList<StoreCategoryMenuVo>());
        for (StoreCategory p : list) {
        	StoreCategoryMenuVo vo = new StoreCategoryMenuVo();
        	vo.setId(p.getId());
        	vo.setParentId(p.getPid());
        	vo.setName(p.getCateName());
        	vo.setCateCode(p.getCateCode());
            map.put(String.valueOf(vo.getId()), vo);
            if (vo.getChildren() == null) {
                vo.setChildren(new ArrayList<StoreCategoryMenuVo>());
            }
            matchs[i++] = vo.getId() + ":" + vo.getParentId();
        }
        for (String match : matchs) {
            id = match.split(":")[0];
            parentId = match.split(":")[1];
            if (null != map.get(parentId)) {
                if (null == map.get(parentId).getChildren()) {
                    map.get(parentId).setChildren(new ArrayList<StoreCategoryMenuVo>());
                }
                map.get(parentId).getChildren().add(map.get(id));
            }
        }
        return map;
    }

}
